package com.xhs.main.controller.xsb;

import com.alibaba.fastjson.JSONObject;
import com.xhs.main.entity.result.LayuiResponseResult;
import com.xhs.main.entity.xsb.Client;
import com.xhs.main.entity.xsb.Contract;
import com.xhs.main.entity.xsb.Plan;
import com.xhs.main.service.xsb.impl.XsbContractServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author hjr
 * @create 2020-10-20-下午 5:44
 */

@RestController
public class ContractController {

    @Autowired
    XsbContractServiceImpl xsbContractServiceImpl;


    @RequestMapping("/addContract")
    public  void add(Contract contract){

        contract.setXsbCoState("制单");

        System.out.println(contract);
        xsbContractServiceImpl.addContract(contract);
    }

    @RequestMapping("/getAllContract")
    public LayuiResponseResult showAllContract(){
        List<Contract> allContract = xsbContractServiceImpl.getAllContract();
//       Map<String,Object> map1 = new HashMap<>();
//        map1.put("code",0);
//        map1.put("msg","成功");
//        map1.put("count",allContract.size());
//        map1.put("data",allContract);

        LayuiResponseResult result = new LayuiResponseResult();
        result.setCode(0);
        result.setMsg("成功");
        result.setCount(allContract.size());
        result.setData(allContract);

        return  result;
    }

    @RequestMapping("/getCon")
    public LayuiResponseResult  showbyVague(@RequestParam("searchParams") String searchParams){
        System.out.println(searchParams);

        Contract contract = JSONObject.parseObject(searchParams, Contract.class);

        JSONObject jsonObject = JSONObject.parseObject(searchParams);
        String xsbCName = (String) jsonObject.get("xsbCName");
        Client client =  new Client();
        client.setXsbCName(xsbCName);

        String xsbDNo = (String) jsonObject.get("xsbDNo");
        Plan plan = new Plan();
        plan.setXsbDNo(xsbDNo);

        //给属性赋值
        contract.setClient(client);
        contract.setPlan(plan);

        List<Contract> byVague = xsbContractServiceImpl.getContractByVague(contract);
        LayuiResponseResult result = new LayuiResponseResult();
        result.setCode(0);
        result.setMsg("成功");
        result.setCount(byVague.size());
        result.setData(byVague);


        return  result;
    }


    @RequestMapping("/updateContract")
    public LayuiResponseResult update(@RequestParam("searchParams") String searchParams){

        System.out.println(searchParams);
        Contract contract = JSONObject.parseObject(searchParams, Contract.class);


        List<Contract> contracts = xsbContractServiceImpl.updateContract(contract);
        System.out.println(contracts);
        LayuiResponseResult result = new LayuiResponseResult();
        result.setCode(0);
        result.setMsg("成功");
        result.setCount(contracts.size());
        result.setData(contracts);

        return result;
    }
}
